package com.example.hostsys.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.annotation.Generated;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Entity
@Data
@JsonIgnoreProperties(value = "hibernateLazyInitializer")
public class User implements UserDetails {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String username;
    private String password;
    private String role;

    private String name;
    private String gender;
    private String icon;

    @OneToOne(mappedBy = "user",fetch = FetchType.LAZY)
    private Student stu;
    @OneToOne(mappedBy = "user",fetch = FetchType.LAZY)
    private HostMan host;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String[] tmpS = role.split(",");
        List<SimpleGrantedAuthority> tmp = new ArrayList<>();
        for(String t:tmpS){
            tmp.add(new SimpleGrantedAuthority(t));
        }
        return tmp;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
